#pragma once
#include "uart/ProtocolSender.h"
#include "uiplayer/ImageAnimView.h"
#include "can/context.h"
#include "logic_utility.hpp"




namespace{
	CanDataCb _s_can_cb;
	int speedStart = 0;
	int speedEnd = 0;
	int tachStart = 0;
	int tachEnd = 0;
	void setTachometer(int rotation)
	{
		mPointerRpmPtr->setTargetAngle(rotation);
	}

	void setSpeedometer(int speed)
	{
		mPointerSpeedPtr->setTargetAngle(speed);

		mSpeedPtr->setText(speed * 280 / 179);
	}

	//连续线性刷新 速度 表盘，返回false表示到达终点
	bool refreshSpeed()
	{
		if(speedEnd == speedStart){
			mSpeedPtr->setText(info1.speed_);
			return false;
		}
		int sign = 1;
		sign = (speedEnd - speedStart) > 0 ? sign : -sign;

		speedStart += sign;

		setSpeedometer(speedStart);

		return true;
	}
	//连续线性刷新 转速 表盘，返回false表示到达终点
	bool refreshTach()
	{
		if(tachEnd == tachStart){
			return false;
		}
		int sign = 1;
		sign = (tachEnd - tachStart) > 0 ? sign : -sign;

		tachStart += sign;

		setTachometer(tachStart);
		return true;
	}

	//线性仪表盘线程
	class Refreshing: public Thread {
	public:
		virtual bool readyToRun() {
			flagTach = true;
			flagSpd = true;
			return true;
		}
		virtual bool threadLoop() {
			if((flagTach || flagSpd))
			{
				flagTach = refreshTach();
				flagSpd = refreshSpeed();
			}else{
				return false;
			}

			usleep(1000 * 10); //50ms
			//返回真，继续下次线程循环
			return true;
		}
		bool flagTach;
		bool flagSpd;
	};
	Refreshing refreshing;
	void vehicle_info1_cb(Vehicle_info1& canData){
		if(info1.rotation_ != canData.rotation_){
			info1.rotation_ = canData.rotation_;
			tachEnd = info1.rotation_ * 180 / 7000;
		    if(!refreshing.isRunning() && tachEnd != tachStart) {
		    	refreshing.run();
		    }
		}
		if(info1.speed_ != canData.speed_){
			info1.speed_ = canData.speed_;
			speedEnd = info1.speed_ * 179 / 280;
		    if(!refreshing.isRunning() && speedEnd != speedStart) {
		    	refreshing.run();
		    }
		}
	}



	void setActivityWin(key_operation_e& operation){
		switch(operation){
		case KEY_UP:{
			EASYUICONTEXT->openActivity("mode1Activity");
		}break;
		case KEY_DOWN:
			EASYUICONTEXT->openActivity("mode1Activity");
			break;
		default:
			return;
		}
	}
	void LongEnterSet(key_operation_e& operation) {
		switch (operation) {
//		case KEY_UP:{
//			EASYUICONTEXT->openActivity("SetupActivity");
//		}break;
		default:
			return;
		}
	}
	void key_value_cb(key_value_t& canData){
		if (canData.pre_status == 1 && canData.status == 0)	//短按
		{
			setActivityWin(canData.operation);
		}else if(canData.pre_status == 1 && canData.status == 2)	//长按
		{
			LongEnterSet(canData.operation);
		}
	}
	void _can_add_cb() {
		_s_can_cb.vehicle_info1_cb = vehicle_info1_cb;
		_s_can_cb.key_value_cb = key_value_cb;
		can::add_cb(&_s_can_cb);
	}

	void _can_remove_cb() {
		can::remove_cb(&_s_can_cb);
	}
	void set_uint(){
		if(unitStatus.s_speed){
			mspeedUnitPtr->setText("mph");
		}else{
			mspeedUnitPtr->setText("km/h");
		}
	}
	void Initialization(){
		tachEnd = info1.rotation_ * 180 / 7000;
		speedEnd = info1.speed_ * 179 / 280;
		setTachometer(tachEnd);
		setSpeedometer(speedEnd);
	}
	void set_font(){
		setCtrlFont(mSpeedPtr);
		setCtrlFont(mspeedUnitPtr);
	}

}


static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {

};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
	set_font();
	AddCallback(set_uint);
	set_uint();
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {
	_can_add_cb();
	Initialization();
}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {
	_can_remove_cb();
}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {
	delCallback(set_uint);
}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */

static bool onUI_Timer(int id){
	switch (id) {
	case 2:

		break;
	default:
		break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onmodeSetActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}

static void onProgressChanged_SeekBarLuminance(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged SeekBarLuminance %d !!!\n", progress);
}
